package cn.e4j.modular.crm.service.impl;

import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwdbT;
import cn.e4j.modular.crm.mapper.GwdbTMapper;
import cn.e4j.modular.system.entity.SysOrg;
import cn.e4j.modular.system.mapper.SysOrgMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-02-01
 */
@Service
public class GwdbTServiceImpl extends ServiceImpl<GwdbTMapper, GwdbT> {

    @Autowired
    private SysOrgMapper sysOrgMapper;

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getListData(Page<Object> page, Dto dto) {
        dto.put("status","0");
        if(!dto.getString("GWDB012_1").equals("")){
            if(dto.getString("GWDB012_2").equals("")){
                dto.put("status","1");
            }else{
                dto.put("status","3");
            }
        }else if(!dto.getString("GWDB012_2").equals("")){
            dto.put("status","2");
        }
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        //渠道权限
        dto.put("GWAES", sysAuthorityService.getGwaesAuthority(dto));
//        List<Map<String, Object>> listData = this.baseMapper.listPage(page, dto);
        List<Map<String, Object>> listData = InvokeUtil.invokeBeanMethod("gwdbTMapper,listPage",page,dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Dto getGwdb001(Dto dto) {
        ShiroUser user = ShiroKit.getUser();
        Dto pDto = Dtos.newDto();
        Date date = new Date();
        pDto.put("GWDB001", "QDJG" + DateUtils.formatDate(date, "yyyyMMddHHmmSSS"));
        pDto.put("GWDB004", DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd));
        pDto.put("GWDB007", user.getID());
        pDto.put("GWDB007_NAME", user.getNAME());
        SysOrg sysOrg = sysOrgMapper.selectById(user.getORG_ID());
        pDto.put("GWDB008", sysOrg.getID());
        pDto.put("GWDB008_NAME", sysOrg.getNAME());
        return pDto;
    }

    @Transactional
    public Dto addData(List<GwdbT> gwdbTList) {
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        ShiroUser user = ShiroKit.getUser();
        //增加新的明细
        for (GwdbT gwdb : gwdbTList) {
            Dto dto = this.baseMapper.getGwdb(user.getDBC(), gwdb.getGWDB001(), gwdb.getGWDB012(), gwdb.getGWDB014());
            if (dto != null) {
                outDto.setAppCode(400);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            } else {
                gwdb.setGWDBENT(user.getDBC());
                gwdb.setGWDBACTI("N");
                gwdb.insert();
            }
        }
        return outDto;
    }

    @Transactional
    public void delete(GwdbT gwdb) {
        UpdateWrapper<GwdbT> gwdbTUpdateWrapper = new UpdateWrapper<>();
        gwdbTUpdateWrapper.eq("GWDBENT", gwdb.getGWDBENT());
        gwdbTUpdateWrapper.eq("GWDB001", gwdb.getGWDB001());
        gwdb.delete(gwdbTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwdbT> gwdbTList) {
        for (GwdbT gwdb : gwdbTList) {
            UpdateWrapper<GwdbT> gwdbTUpdateWrapper = new UpdateWrapper<>();
            gwdbTUpdateWrapper.eq("GWDBENT", gwdb.getGWDBENT());
            gwdbTUpdateWrapper.eq("GWDB001", gwdb.getGWDB001());
            gwdb.delete(gwdbTUpdateWrapper);
        }
    }

    @Transactional
    public void check(List<GwdbT> gwdbTList) {
        ShiroUser user = ShiroKit.getUser();
        for (GwdbT gwdb : gwdbTList) {
            UpdateWrapper<GwdbT> gwdbTUpdateWrapper = new UpdateWrapper<>();
            gwdbTUpdateWrapper.eq("GWDBENT", gwdb.getGWDBENT());
            gwdbTUpdateWrapper.eq("GWDB001", gwdb.getGWDB001());
            gwdb.setGWDBACTI("Y");
            gwdb.setGWDB009(user.getID());
            gwdb.setGWDB010(LocalDateTime.now());
            gwdb.update(gwdbTUpdateWrapper);
        }
    }

    @Transactional
    public void unCheck(List<GwdbT> gwdbTList) {
        ShiroUser user = ShiroKit.getUser();
        for (GwdbT gwdb : gwdbTList) {
            Dto dto = Dtos.newDto();
            dto.put("GWDBENT", gwdb.getGWDBENT());
            dto.put("GWDB001", gwdb.getGWDB001());
            dto.put("GWDBACTI", "N");
            dto.put("GWDBMODU", user.getID());
            this.baseMapper.updateGwdb(dto);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwdb(Dto dto) {
        List<Map<String, Object>> gwdbDtos = this.baseMapper.listGwdb(dto);
        return gwdbDtos;
    }

    @Transactional
    public Dto editData(List<GwdbT> gwdbTList) {
        ShiroUser user = ShiroKit.getUser();
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);

        GwdbT gwdb1 = new GwdbT();
        UpdateWrapper<GwdbT> gwdbTUpdateWrapper = new UpdateWrapper<>();
        gwdbTUpdateWrapper.eq("GWDBENT", user.getDBC());
        gwdbTUpdateWrapper.eq("GWDB001", gwdbTList.get(0).getGWDB001());
        gwdb1.delete(gwdbTUpdateWrapper);

        //增加新的明细
        for (GwdbT gwdb : gwdbTList) {
            Dto dto = this.baseMapper.getGwdb(user.getDBC(), gwdb.getGWDB001(), gwdb.getGWDB012(), gwdb.getGWDB014());
            if (dto != null) {
                outDto.setAppCode(400);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            } else {
                gwdb.setGWDBENT(user.getDBC());
                gwdb.setGWDBACTI("N");
                gwdb.insert();
            }
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    public Dto getGwdb(Dto dto) {
        ShiroUser user = ShiroKit.getUser();
        Dto pDto = this.baseMapper.getGwdb2(dto);
        return pDto;
    }

    @Transactional(readOnly = true)
    public Dto getGwakl(String GWAKLENT, String GWAKL001, String GWAKL002) {
        ShiroUser user = ShiroKit.getUser();
        Dto pDto = this.baseMapper.getGwakl(GWAKLENT,GWAKL001,GWAKL002);
        return pDto;
    }

    @Transactional(readOnly = true)
    public Dto getGwael(String GWAELENT, String GWAEL001, String GWAEL002) {
        ShiroUser user = ShiroKit.getUser();
        Dto pDto = this.baseMapper.getGwael(GWAELENT,GWAEL001,GWAEL002);
        return pDto;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwam(Page<Object> page, Dto dto) {
        dto.put("status","0");
        if(!dto.getString("GWDB012_1").equals("")){
            if(dto.getString("GWDB012_2").equals("")){
                dto.put("status","1");
            }else{
                dto.put("status","3");
            }
        }else if(!dto.getString("GWDB012_2").equals("")){
            dto.put("status","2");
        }
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        //渠道权限
        dto.put("GWAES", sysAuthorityService.getGwaesAuthority(dto));
//        List<Map<String, Object>> listData = this.baseMapper.listGwam(page, dto);
        List<Map<String, Object>> listData = InvokeUtil.invokeBeanMethod("gwdbTMapper,listGwam",page,dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwamList(Dto dto) {
        BigDecimal gwak062 = new BigDecimal(1);
        if(dto.getString("GWAK061").equals("Y")) {
            Dto gwak = this.baseMapper.getGwak(dto);
            if(gwak != null){
                gwak062  = new BigDecimal(1).add(gwak.getBigDecimal("GWAK062").divide(new BigDecimal(100)));
            }
        }
        dto.put("gwak062", gwak062);
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        //渠道权限
        dto.put("GWAES", sysAuthorityService.getGwaesAuthority(dto));
//        dto.put("DATE",DateUtils.getDate("yyyy-MM-dd"));
        dto.put("DATE",LocalDate.now());
        List<Dto> list = null;
        List<Map<String, Object>> listData = null;
        if(!dto.getString("GWDB012").isEmpty()){
            if(this.baseMapper.listGwamlist(dto).size() > 0){
                list = this.baseMapper.listGwamlist(dto);
            }
            dto.put("GWAMS", list);
            listData = InvokeUtil.invokeBeanMethod("gwdbTMapper,listGwamList",dto);
        }
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwamDetail(Dto dto) {
        BigDecimal gwak062 = new BigDecimal(1);
        if(dto.getString("GWAK061").equals("Y")) {
            Dto gwak = this.baseMapper.getGwak(dto);
            if(gwak != null){
                gwak062  = new BigDecimal(1).add(gwak.getBigDecimal("GWAK062").divide(new BigDecimal(100)));
            }
        }
        dto.put("gwak062", gwak062);
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        //渠道权限
        dto.put("GWAES", sysAuthorityService.getGwaesAuthority(dto));
        dto.put("DATE",DateUtils.getDate("yyyy-MM-dd"));
        List<Dto> list = null;
        List<Dto> temp = new ArrayList<>();
        temp=InvokeUtil.invokeBeanMethod("gwdbTMapper,listGwamList",dto);
        if(temp.size() > 0){
            list = temp;
        }
        dto.put("GWAMS", list);
        List<Dto> listData = InvokeUtil.invokeBeanMethod("gwdbTMapper,getGwamDetail",dto);
        for(Dto pDto : listData){
            pDto.put("ENT", dto.getString("ENT"));
            List<Dto> gwan = this.baseMapper.getGwaj(pDto);
            pDto.put("child", gwan);
        }
        return listData;
    }

}
